package cn.huiyunche.base.service.interfaces.impl;

import cn.huiyunche.base.service.interfaces.CWaybillService;
import cn.huiyunche.base.service.interfaces.SOrderTrailService;
import cn.huiyunche.base.service.interfaces.UserService;
import cn.huiyunche.base.service.mappers.SOrderTrailMapper;
import cn.huiyunche.base.service.mappers.ext.WaybillTrailExtMapper;
import cn.huiyunche.base.service.model.*;
import cn.huiyunche.base.service.vo.UserVo;
import cn.huiyunche.tools.basic.exceptions.BusinessException;
import com.alibaba.fastjson.JSON;
import org.apache.commons.beanutils.BeanUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class SOrderTrailServiceImpl implements SOrderTrailService {

    private static final Logger logger = LoggerFactory.getLogger(SOrderTrailServiceImpl.class);

    @Autowired
    private SOrderTrailMapper sOrderTrailMapper = null;

    @Autowired
    private WaybillTrailExtMapper waybillTrailExtMapper;

    @Autowired
    private CWaybillService waybillService = null;

    @Autowired
    private UserService userService = null;

    @Override
    public void add(SOrderTrail sOrderTrail) throws Exception {
        UserVo vo = this.getUserService().getCurrentUserVo();
        SUser user = this.getUserService().getById(vo.getId());
        if (null == user) {
            logger.error("select user info error");
            throw new BusinessException("未找到当前用户信息");
        }
        sOrderTrail.setDriverId(user.getId());
        sOrderTrail.setDriverName(user.getRealName());

        //查询运单信息，保存位置信息
        //添加运单信息
        List<Integer> statusList = new ArrayList<Integer>();
//		statusList.add(SOrderStatusEnum.ALREADYSTART.getValue());
//		statusList.add(SOrderStatusEnum.INTRANSIT.getValue());
        List<CWaybill> waybills = this.getWaybillService().selectByUseridAndStatus(user.getId(), statusList);

        // 未查询到运单信息只保存位置信息
        if (null == waybills || waybills.size() == 0) {
            logger.info("add waybill trail,not find waybill. params:{}", sOrderTrail);
            this.getSOrderTrailMapper().insertSelective(sOrderTrail);
        } else {
            logger.info("add waybill trail,waybill:{}", JSON.toJSONString(waybills));
            List<CWaybillTrail> waybillTrails = new ArrayList<CWaybillTrail>();
            for (int i = 0; i < waybills.size(); i++) {
                CWaybill waybill = waybills.get(i);
                CWaybillTrail newWayBillTrail = new CWaybillTrail();
                BeanUtils.copyProperties(newWayBillTrail, sOrderTrail);
                newWayBillTrail.setOrderId(waybill.getOrderId());
                newWayBillTrail.setWaybillId(waybill.getId());
                waybillTrails.add(newWayBillTrail);
            }
            waybillTrailExtMapper.batchInsertWaybillTrails(waybillTrails);
            logger.info("add waybill trail,waybillTrail:{}", JSON.toJSONString(waybillTrails));
        }
    }

    @Override
    public List<SOrderTrail> selectByWaybillid(Long waybillID) {
        SOrderTrailExample sOrderTrailExample = new SOrderTrailExample();
        sOrderTrailExample.setOrderByClause("id desc");
        sOrderTrailExample.setLimitStart(0);
        sOrderTrailExample.setLimitEnd(1);
        sOrderTrailExample.createCriteria().andOrderIdEqualTo(waybillID);
        return sOrderTrailMapper.selectByExample(sOrderTrailExample);
    }

    private SOrderTrailMapper getSOrderTrailMapper() {
        return this.sOrderTrailMapper;
    }

    private UserService getUserService() {
        return this.userService;
    }

    public CWaybillService getWaybillService() {
        return waybillService;
    }

}
